/* Includes ------------------------------------------------------------------*/
#include "KeyScan.h"
#include "var.h"
#include "display.h"
#include "TimeEvent.h"
#include "string.h"
#include "nv_flash.h"
#include "ThermostatTask.h"
#include "comp.h"
// #include "multi_button.h"
/* Private define ------------------------------------------------------------*/

/*********************************************************************
 * MACROS
 */
/*********************************************************************
 * TYPEDEFS
 */

/*********************************************************************
 * CONSTANTS
 */

/*********************************************************************
 * GLOBAL VARIABLES
 */

/*********************************************************************
 * @fn      void KeyScan(void)
 *
 * @brief   按键扫描处理
 *
 * @param   none
 *
 * @return  none
 */
void KeyScanTask(void)
{
    if (Flag1.OneBit.KeyScan == 1)
    {
        /********读取IO口电平***********/
        KeyBuff.OneBit.Decrease = P0_7; // 下键
        KeyBuff.OneBit.Increase = P0_6; // 上键
        KeyBuff.OneBit.Set = P3_2;      // SET键

        KeyChangedBuff.allBits = LastTimeKey.allBits;
        KeyChangedBuff.allBits = KeyBuff.allBits ^ KeyChangedBuff.allBits;

        for (keyCount = 0; keyCount < 3; keyCount++)
        {
            if ((KeyChangedBuff.allBits & (1 << keyCount)) && ((~KeyBuff.allBits) & (1 << keyCount)))
            {
                status = KeyBuff.allBits | (1 << keyCount);
                if (status == 0xFF && KeyNum == 0xFF)
                {
                    KeyNum = keyCount;
                    FF_PressCount = 0;
                    Flag1.OneBit.PressFlutterFree = 1; // 开启按下去抖检测
                }
            }
            else if ((KeyChangedBuff.allBits & (1 << keyCount)) && (KeyBuff.allBits & (1 << keyCount)))
            {
                if (KeyNum == keyCount)
                {
                    FF_ReleaseCount = 0;
                    Flag1.OneBit.ReleaseFlutterFree = 1; // 开启释放去抖检测
                }
            }
        }
        LastTimeKey.allBits = KeyBuff.allBits;

        KeyPressHandler();
        KeyLongPressHandler();
        KeyReleaseHandler();
    }
}

/*********************************************************************
 * @fn      void TMR0_Hander(void)
 *
 * @brief   定时器处理函数
 *
 * @param   none
 *
 * @return  none
 */
void KeyScan_Ticks(void) // 10ms中断
{
    /******************按键按下去抖相关******************/
    if (Flag1.OneBit.PressFlutterFree == ENABLE) // 按下去抖检测
    {
        if ((~KeyBuff.allBits) & (1 << KeyNum))
        {
            FF_ReleaseCount = 0;
            FF_PressCount++;
            if (FF_PressCount > 50U)
            {
                FF_PressCount = 0;
                Flag1.OneBit.PressFlutterFree = 0;
                Flag1.OneBit.LongScan = 1; // 开启长按

                //  tPara.LPCount1 = 0;
                //  tPara.LPCount2 = 0;
                //  tPara.LPCount3 = 0;
                //  tPara.LPCount4 = 0;

                FlagKeyPress.allBits |= (1 << KeyNum); // 产生按下标志
                KeyStatus = Press;
            }
        }
        else
        {
            FF_PressCount = 0;
            Flag1.OneBit.PressFlutterFree = 0;
            KeyNum = 0xFF;
        }
    }
    /******************按键释放去抖相关******************/
    if (Flag1.OneBit.ReleaseFlutterFree == ENABLE) // 释放去抖检测
    {
        if (KeyBuff.allBits & (1 << KeyNum))
        {
            FF_PressCount = 0;
            FF_ReleaseCount++;
            if (FF_ReleaseCount > 50U) // 50ms
            {
                FF_ReleaseCount = 0;
                Flag1.OneBit.ReleaseFlutterFree = 0;
                Flag1.OneBit.LongScan = 0; // 关闭长按

                // FlagLongKey.allBits = 0;
                FlagKeyRelease.allBits |= (1 << KeyNum); // 产生释放标志
                KeyNum = 0xFF;
                KeyStatus = Release;
            }
        }
        else
        {
            FF_ReleaseCount = 0;
        }
    }

    /******************按键长按相关******************/
    if (Flag1.OneBit.LongScan == ENABLE) // 长按扫描
    {
        if (KeyBuff.OneBit.Set == 0 && KeyBuff.OneBit.Increase == 1 && KeyBuff.OneBit.Decrease == 1) // 长按set（0页面暂无，1和2页面下保存）
        {
            //            if (m_PageInfor.m_Type == 0x01 || m_PageInfor.m_Type == 0x02 || m_PageInfor.m_Type == 0x03) //主界面配置下产生的确认事件
            //            {
            // tPara.LPCount1 = 0;
            tPara.LPCount2 = 0;
            tPara.LPCount3 = 0;
            tPara.LPCount4 = 0;
            tPara.LPCount5 = 0;
            tPara.LPCount6 = 0;
            tPara.LPCount1++;
            if (tPara.LPCount1 > 2500UL) // 3s 进入下一级菜单
            {
                // Flag1.OneBit.Key1_HoldStart = 1;
                Flag1.OneBit.LongScan = 0; // 长按扫描关闭
                tPara.LPCount1 = 0;

                LongKeyEvt = 1; // 产生长按标致
            }
            //            }
        }
        else if (KeyBuff.OneBit.Set == 1 && KeyBuff.OneBit.Increase == 0 && KeyBuff.OneBit.Decrease == 1) // 长按+（ 0页面下暂无，1和2页面下快进）
        {
            if (m_PageInfor.m_Type == 0x00 || m_PageInfor.m_Type == 0x01 || m_PageInfor.m_Type == 0x02) // 主界面配置内容页面
            {
                if (m_PageInfor.m_Type == 0x00)
                    LongPressTime = 3000;
                tPara.LPCount1 = 0;
                // tPara.LPCount2 = 0;
                tPara.LPCount3 = 0;
                tPara.LPCount4 = 0;
                tPara.LPCount5 = 0;
                tPara.LPCount6 = 0;
                tPara.LPCount2++;
                if (tPara.LPCount2 > LongPressTime) // 100ms产生一个变化
                {
                    tPara.LPCount2 = 0;

                    LongPressTime = 100;
                    Flag2.OneBit.Blink = DISPLAY;
                    LongKeyEvt = 2; // 产生长按标致
                }
            }
        }
        else if (KeyBuff.OneBit.Set && KeyBuff.OneBit.Increase && KeyBuff.OneBit.Decrease == 0) // 长按-（ 0页面下强制除霜开启，1和2页面下快进）
        {
            if (m_PageInfor.m_Type == 0x00 || m_PageInfor.m_Type == 0x01 || m_PageInfor.m_Type == 0x02)
            {
                if (m_PageInfor.m_Type == 0x00)
                    LongPressTime = 3000;
                tPara.LPCount1 = 0;
                tPara.LPCount2 = 0;
                // tPara.LPCount3 = 0;
                tPara.LPCount4 = 0;
                tPara.LPCount5 = 0;
                tPara.LPCount6 = 0;
                tPara.LPCount3++;
                if (tPara.LPCount3 > LongPressTime) //
                {
                    tPara.LPCount3 = 0;

                    LongPressTime = 100;
                    Flag2.OneBit.Blink = DISPLAY;
                    LongKeyEvt = 3; // 产生长按标致

                    // Flag1.OneBit.Key1_HoldStart = 1;
                }
            }
        }
        else if (KeyBuff.OneBit.Set == 0 && KeyBuff.OneBit.Increase == 1 && KeyBuff.OneBit.Decrease == 0) // 长按set&-  （0页面下进入参数配置栏）
        {
            if (m_PageInfor.m_Type == 0x00) // 主界面配置下产生的确认事件
            {
                tPara.LPCount1 = 0;
                tPara.LPCount2 = 0;
                tPara.LPCount3 = 0;
                // tPara.LPCount4 = 0;
                tPara.LPCount5 = 0;
                tPara.LPCount6 = 0;
                tPara.LPCount4++;
                if (tPara.LPCount4 > 2500UL) // 3s 进入下一级菜单
                {
                    // Flag1.OneBit.LongScan = 0; // 长按扫描关闭
                    // tPara.LPCount4 = 0;

                    LongKeyEvt = 4; // 产生长按标致
                }
            }
            else if (m_PageInfor.m_Type == 0x01)
            {
                tPara.LPCount1 = 0;
                tPara.LPCount2 = 0;
                tPara.LPCount3 = 0;
                tPara.LPCount5 = 0;
                tPara.LPCount6 = 0;
                tPara.LPCount4++;
                if (tPara.LPCount4 > 9000UL) // 10s 进入下一级菜单
                {
                    Flag1.OneBit.LongScan = 0; // 长按扫描关闭
                    tPara.LPCount4 = 0;

                    LongKeyEvt = 7; // 产生长按标致
                }
            }
        }
        else if (KeyBuff.OneBit.Set == 1 && KeyBuff.OneBit.Increase == 0 && KeyBuff.OneBit.Decrease == 0) // 长按+&-,（0页面下进入温度设置）
        {
            if (m_PageInfor.m_Type == 0x00 || m_PageInfor.m_Type == 0x03)
            {
                tPara.LPCount1 = 0;
                tPara.LPCount2 = 0;
                tPara.LPCount3 = 0;
                tPara.LPCount4 = 0;
                tPara.LPCount6 = 0;
                tPara.LPCount5++;
                if (tPara.LPCount5 > 2500UL) // 3s 进入下一级菜单
                {
                    Flag1.OneBit.LongScan = 0; // 长按扫描关闭
                    tPara.LPCount5 = 0;

                    if (m_PageInfor.m_Type == 0x00)
                    {
                        LongKeyEvt = 5; // 产生长按标致
                    }
                    else if (m_PageInfor.m_Type == 0x03)
                    {
                        LongKeyEvt = 8; // 产生长按标致(重新启动事件)
                    }
                }
            }
        }
        else if (KeyBuff.OneBit.Set == 0 && KeyBuff.OneBit.Increase == 0 && KeyBuff.OneBit.Decrease == 1) // 长按set&+,（强制除霜）
        {
            if (m_PageInfor.m_Type == 0x00)
            {
                tPara.LPCount1 = 0;
                tPara.LPCount2 = 0;
                tPara.LPCount3 = 0;
                tPara.LPCount4 = 0;
                tPara.LPCount5 = 0;
                tPara.LPCount6++;
                if (tPara.LPCount6 > 5000UL) //
                {
                    Flag1.OneBit.LongScan = 0; // 长按扫描关闭
                    tPara.LPCount6 = 0;

                    LongKeyEvt = 6; // 产生长按标致
                }
            }
        }
        else
        {
            Flag1.OneBit.LongScan = 0; // 长按扫描关闭
            tPara.LPCount1 = 0;
            tPara.LPCount2 = 0;
            tPara.LPCount3 = 0;
            tPara.LPCount4 = 0;
            tPara.LPCount5 = 0;
            tPara.LPCount6 = 0;
            LongKeyEvt = 0;
        }
    }
    else
    {
        tPara.LPCount1 = 0;
        tPara.LPCount2 = 0;
        tPara.LPCount3 = 0;
        tPara.LPCount4 = 0;
        tPara.LPCount5 = 0;
        tPara.LPCount6 = 0;
    }
}

/*********************************************************************
 * @fn      void KeyPressHandler(void)
 *
 * @brief   按键按下处理
 *
 * @param   none
 *
 * @return  none
 */

void KeyPressHandler(void)
{
    if (FlagKeyPress.allBits)
    {
        PressPageType = m_PageInfor.m_Type; // 记录当前按下时的页面
        LongPressTime = 2500;

        switch (m_PageInfor.m_Type)
        {
        case 0x00: // 主界面,显示温度
        {
            if (FlagKeyPress.OneBit.Decrease == 1)
            {
                if (cfgPara[CoolingMode].defrosteCycleTime != 0 && cfgPara[CoolingMode].defrosteType == 1)
                {
                    if (ErrorState.OneBit.Ntc1Error == 0) // 化霜传感器正常
                        DisplayTempValue(DefrostTemp);
                    else
                        DisplayDatasSymbol(Code_E, Num_1);
                }
                else
                {
                    displayCode[2] = Code_y;
                    displayCode[1] = Code_y;
                    displayCode[0] = Code_y;
                    DisplayAllSymbol(displayCode);
                }
                Flag3.OneBit.defrosteTempShow = TRUE;
                displayState = 0;
                Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 500, ENABLE, 0, DisplayRefurbish); // 500ms刷新
            }

            // if (FlagKeyPress.OneBit.Increase == 1) //按-,查看化霜传感器数据
            // {
            //     Flag1.OneBit.Key1_HoldStart = 0;
            // }

            if (Flag2.OneBit.Lock == 0)
            {
                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
            }
        }
        break;
        case 0x01: // 参数列表界面
        {
            // if (FlagKeyPress.OneBit.Decrease == 1 || FlagKeyPress.OneBit.Increase == 1) //按-
            // {
            //     LongPressTime = 200;
            // }

            if (FlagKeyPress.OneBit.Decrease == 1) // 按-
            {
                LongPressTime = 200;
                if (m_PageInfor.m_CfgPg == 0x00) ////单单温度设置页面下
                {
                    if (Flag3.OneBit.pressCount == 0)
                    {
                        Flag3.OneBit.pressCount = 1; // 第二按键
                    }
                    else
                    {
                        Flag3.OneBit.pressCount = 3; // 非测试模式按键流程
                    }
                }
            }

            if (FlagKeyPress.OneBit.Increase == 1) // 按+
            {
                LongPressTime = 200;
                if (m_PageInfor.m_CfgPg == 0x00) // 单单温度设置页面下
                {
                    if (Flag3.OneBit.pressCount == 1)
                    {
                        Flag3.OneBit.pressCount = 2; // 第一按键
                    }
                    else
                    {
                        Flag3.OneBit.pressCount = 3; // 非测试模式按键流程
                    }
                }
            }
            /***************************以上为进入测试操作流程***************************/
            Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件

            if (FlagKeyPress.OneBit.Set == 1) // 按Set,进入参数数据界面，St功能页面下依次操作,+,- set进入到测试模式
            {
                if (Flag2.OneBit.Rt0Set == 1) // St单功能页面下依次操作
                {
                    // 正常温度参数配置参数页面
                    m_PageInfor.m_Type = 0x02; // 参数设置栏
                    DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
                }
                else // P0 组态参数栏下set进入
                {
                    if (Flag3.OneBit.pressCount == 2)
                    {
                        /********************************** 进入到测试菜单页面下 **********************************/
                        Flag3.OneBit.TestMode = 1; // 当前为测试模式
                        m_PageInfor.m_Type = 0x03; // 测试页面下
                        m_PageInfor.m_CfgPg = 0;
                        DisplayDatasSymbol(Code_t, Code_t);
                        Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 10 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt);
                    }
                    else
                    {
                        /************************************** P0 组态参数栏下set进入 **************************************/
                        // 正常配置参数页面
                        if (m_PageInfor.m_CfgPg == 0x00) // P0栏
                        {
                            if (Flag2.OneBit.PwRight != 1) // 密码验证过
                            {
                                DuplicatePara.password = 0; // 密码初始值
                            }
                        }
                        m_PageInfor.m_Type = 0x02; // 参数设置栏
                        DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);
                    }
                }
                displayState = 0;
                Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 500, ENABLE, 0, DisplayRefurbish); // 500ms刷新
            }
        }
        break;

        case 0x02: ////参数内容页面
        {
            if (FlagKeyPress.OneBit.Increase == 1) // 按+
            {
                LongPressTime = 200;
                if (Flag2.OneBit.Lock == 0)
                {
                    if (DuplicateData == 0)
                    {
                        DuplicateData = 1;
                    }
                    else
                    {
                        DuplicateData = 0;
                    }

                    Flag2.OneBit.Rt0Set = 1;
                    Flag2.OneBit.PwRight = 0; // 密码状态归0

                    m_PageInfor.m_Type = 0x02;                                                         // 参数设置栏 //进入配置栏（P0显示）
                    m_PageInfor.m_CfgPg = 0x01;                                                        // 温度栏
                    memcpy((uint8_t *)&DuplicatePara.St, (uint8_t *)&cfgPara[DuplicateData].St, 0x03); // 复制温度备用数据
                    St_ParasRangeUpate_Evt(DuplicateData);
                    DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);

                    Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
                }
            }

            if (FlagKeyPress.OneBit.Decrease == 1) // 按-
            {
                LongPressTime = 200;
            }

            // if (Flag2.OneBit.Rt0Set == 0)
            //     Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], Minute, ENABLE, 0, ExitCfgPageAndSavePara_Evt);
            // else
            Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
            if (FlagKeyPress.OneBit.Set == 1)                                                                          // 按Set,返回到列表栏
            {
                if (Flag2.OneBit.Rt0Set == 0)
                {
                    if (m_PageInfor.m_CfgPg == 0x00) // P0
                    {
                        if (Flag2.OneBit.PwRight == 0) // 密码未验证过，进行密码验证逻辑
                        {
                            if (DuplicatePara.password == cfgPara[CoolingMode].password) // 密码验证通过  55
                            {
                                Flag2.OneBit.PwRight = 1;                         // 密码验证通过
                                m_PageInfor.m_CfgPg = 0x01;                       // P0不显示，St参数栏开始
                                m_PageInfor.m_Type = 0x01;                        // 进入数据栏（P0显示）
                                DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]); // 显示参数菜单栏
                            }
                            else if (DuplicatePara.password == 99) // 查看版本
                            {
                                displayCode[2] = NumCode[ver[0]] | Code_dp;
                                displayCode[1] = NumCode[ver[1]] | Code_dp;
                                displayCode[0] = NumCode[ver[2]] | Code_dp;
                                DisplayAllSymbol(displayCode);

                                Flag3.OneBit.TestMode = 0; // 当前为测试模式
                                m_PageInfor.m_Type = 0x03; // 测试页面下
                                m_PageInfor.m_CfgPg = 0;
                                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 5 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
                            }
                            else // 密码验证不通过，返回主显示界面
                            {
                                BackHomePage_Evt();
                                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
                            }
                        }
                        else // 密码验证过后,操作set返回到01组态配置栏
                        {
                            m_PageInfor.m_Type = 0x01;                        // 进入数据栏（P0显示）
                            DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]); // 显示参数菜单栏
                        }
                    }
                    else // 其他参数返回到配置有页面
                    {
                        m_PageInfor.m_Type = 0x01;                        // 进入数据栏（P0显示）
                        DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]); // 显示参数菜单栏
                        St_ParasRangeUpate_Evt(DuplicateData);
                    }
                }
                else // 单单温度设置保存后退出
                {
                    /************************ 单单温度设置保存后退出 ************************/
                    if (cfgPara[DuplicateData].St != DuplicatePara.St || DuplicateData != CoolingMode)
                    {
                        cfgPara[DuplicateData].St = DuplicatePara.St;
                        if (QuickFrozenState == q_Idle)
                        {
                            CoolingMode = DuplicateData;
                        }
                        // CoolingMode = DuplicateData;

                        Flag1.OneBit.WriteEEP = 1; // 保存参数
                        Flag3.OneBit.eepThcfg = 1;
                        Flag3.OneBit.keyStCfg = 1;

                        Flag2.OneBit.RealDisplayDelay = 1; // 保存参数后进行假显示
                        tPara.Count5s = 0;
                        // Timer_Event_Set(&timer_10msDB[RealDisplayDelay_TaskId], 5 * Second, ENABLE, 0, RealDisplay_Evt); //保存参数后进行假显示
                    }
                    Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 10, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
                }
                displayState = 0;
                Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 100, ENABLE, 0, DisplayRefurbish); // 500ms刷新
            }
        }
        break;

        case 0x03:
            if (Flag3.OneBit.TestMode == 1)
            {
                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 5 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt);
                if (FlagKeyPress.OneBit.Set == 1) // 按Set,返回到列表栏  t0网络 t1柜温传感器 t2化霜传感器  t3压缩机  t4加热器  t5照明
                {
                    Test_Run_Evt();
                }
            }
            break;

        default:
            break;
        }
        FlagKeyPress.allBits = 0;
    }
}

/*********************************************************************
 * @fn      void KeyReleaseHandler(void)
 *
 * @brief   按键释放处理
 *
 * @param   none
 *
 * @return  none
 */
void KeyReleaseHandler(void)
{
    if (FlagKeyRelease.allBits)
    {
        Flag1.OneBit.PressFlutterFree = 0;
        Flag1.OneBit.ReleaseFlutterFree = 0;

        /********释放处理*********/
        if (PressPageType == m_PageInfor.m_Type) // 同个页面逻辑下的完整按键
        {
            switch (m_PageInfor.m_Type)
            {
            case 0x00:
            {
                // if (FlagKeyRelease.OneBit.Increase == 1) //按+
                // {
                //     if (Flag1.OneBit.Key1_HoldStart == 0) // 3秒内短按,设置温度操作
                //     {
                //     }
                // }

                if (FlagKeyRelease.OneBit.Decrease == 1) //-释放
                {
                    Flag3.OneBit.defrosteTempShow = FALSE;
                    displayState = 0;
                    Timer_Event_Set(&timer_10msDB[DisplayRefurbish_TaskId], 50, ENABLE, 0, DisplayRefurbish); // 2500ms
                }
            }
            break;

            case 0x01: // 参数列表名称变化
            {
                LongPressTime = 200;
                if (Flag2.OneBit.Rt0Set == 0) // 组态参数栏下配置
                {
                    if (FlagKeyRelease.OneBit.Decrease == 1) // 按-
                    {
                        if (Flag2.OneBit.PwRight != 0) // 密码验证通过
                        {
                            if (m_PageInfor.m_CfgPg > 1)
                                m_PageInfor.m_CfgPg--; // 翻页--
                        }
                        else
                        {
                            m_PageInfor.m_CfgPg = 0;
                        }
                        DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);
                    }

                    if (FlagKeyRelease.OneBit.Increase == 1) // 按+
                    {
                        if (Flag2.OneBit.PwRight != 0) // 密码验证通过
                        {
                            if (m_PageInfor.m_CfgPg < (CfglistNum - 1))
                                m_PageInfor.m_CfgPg++; // 翻页++
                        }
                        else
                        {
                            m_PageInfor.m_CfgPg = 0;
                        }
                        DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);
                    }
                }
            }
            break;

            case 0x02: // 参数内容配置
            {
                if (Flag2.OneBit.Lock == 1)
                {
                    LongPressTime = 200;
                    if (FlagKeyRelease.OneBit.Decrease == 1) // 按-
                    {
                        Flag2.OneBit.Blink = DISPLAY;
                        Page_cfgData_Decrease_Evt(); //-
                    }

                    if (FlagKeyRelease.OneBit.Increase == 1) // 按+
                    {
                        Flag2.OneBit.Blink = DISPLAY;
                        Page_cfgData_Increase_Evt(); //+
                    }
                }
                else // 解锁下，修改温度设定值
                {
                    LongPressTime = 200;
                    if (FlagKeyRelease.OneBit.Decrease == 1) // 按-
                    {
                        Flag2.OneBit.Blink = DISPLAY;
                        Page_cfgData_Increase2_Evt(); //-
                    }
                }
            }
            break;
            case 0x03: // 密码位选择操作
                break;
            }
        }
        FlagKeyRelease.allBits = 0;
    }
}

/*********************************************************************
 * @fn      void KeyLongPressHandler(void)
 *
 * @brief   按键长按下处理
 *
 * @param   none
 *
 * @return  none
 */
void KeyLongPressHandler(void)
{
    if (LongKeyEvt)
    {
        switch (LongKeyEvt)
        {
        case 1:
            if (m_PageInfor.m_Type == 0x00)
            {
                if (PressPageType == m_PageInfor.m_Type)
                {
                    if (Flag3.OneBit.factoryMode == 0) // 速冻模式
                    {
                        if (cfgPara[CoolingMode].St < -12 && CoolingMode == 0) // 设定温度小于 -12 才可以进行速冻模式 && 冷冻模式下
                        {
                            if (QuickFrozenState != quickFrozening) // 进入速冻模式
                            {
                                if (ErrorState.OneBit.Ntc0Error == 0 && ErrorState.OneBit.Ntc1Error == 0) // 柜内传感器正常下进入化霜
                                {
                                    tPara.quickFrozenTimeCount = 0;
                                    if (DefrostState == defrostingCycle)
                                    {
                                        QuickFrozenState = quickFrozening;
                                    }
                                }
                            }
                            else
                            {
                                QuickFrozenState = q_Idle;
                            }
                        }
                    }
                }
            }
            else if (m_PageInfor.m_Type == 0x01 || m_PageInfor.m_Type == 0x02) // 配置栏，确认事件
            {
                /************************************* 保存测试参数 ******************************************/
                if (cfgPara[CoolingMode].St != DuplicatePara.St) // 与原先设定温度B不一致，就强制退出修正模式。
                {
                    Flag3.OneBit.DisplayState = Real;
                    tPara.Count2s = 0;
                    tPara.Count30m = 0;
                }
                else // 参数设置后,也强制退出修正模式。进入实时显示。
                {
                    if (m_PageInfor.m_CfgPg > 1)
                    {
                        Flag3.OneBit.DisplayState = Real;
                        tPara.Count2s = 0;
                        tPara.Count30m = 0;
                    }
                }

                if (memcmp((uint8_t *)&cfgPara[CoolingMode].password, (uint8_t *)&DuplicatePara.password, CfglistNum) != NULL) // 数据变化了
                {
                    if (Flag2.OneBit.PwRight != 0) // 密码验证通过
                    {
                        memcpy((uint8_t *)&cfgPara[CoolingMode].password, (uint8_t *)&DuplicatePara.password, CfglistNum); // 保存数据
                    }
                    else
                        cfgPara[CoolingMode].St = DuplicatePara.St;

                    St_ParasRangeUpate_Evt(CoolingMode);

                    Flag1.OneBit.WriteEEP = 1;
                    Flag3.OneBit.eepThcfg = 1;
                    Flag3.OneBit.keyStCfg = 1;
                }
                BackHomePage_Evt();
                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt);
            }
            break;

            /*********************** 数据快速变化 ***********************/
        case 2: // 参数修改时长按+，快速变化
            if (m_PageInfor.m_Type == 0x01)
            {
                if (Flag2.OneBit.Rt0Set == 0) // 翻页
                {
                    if (Flag2.OneBit.PwRight != 0) // 密码验证通过
                    {
                        if (m_PageInfor.m_CfgPg < (CfglistNum - 1))
                            m_PageInfor.m_CfgPg++; // 翻页++
                    }
                    DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);
                }
            }
            else if (m_PageInfor.m_Type == 0x02)
            {
                if (Flag2.OneBit.Lock == 1)
                {
                    Page_cfgData_Increase_Evt(); //+
                }
                else
                {
                }
                // Page_cfgData_Increase_Evt(); //+
            }
            break;

        case 3: // 参数修改时长按-，快速变化
            if (m_PageInfor.m_Type == 0x01)
            {
                if (Flag2.OneBit.Rt0Set == 0) // 非温度设置下
                {
                    if (Flag2.OneBit.PwRight != 0) // 密码验证通过
                    {
                        if (m_PageInfor.m_CfgPg > 1)
                            m_PageInfor.m_CfgPg--; // 翻页--
                    }
                    DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);
                }
            }
            else if (m_PageInfor.m_Type == 0x02)
            {
                if (Flag2.OneBit.Lock == 1)
                {
                    Page_cfgData_Decrease_Evt(); //-
                }
                else // 解锁下，快速操作
                {
                    Page_cfgData_Increase2_Evt();
                }

                // Page_cfgData_Decrease_Evt();//-
            }
            break;

            /*********************** 首次进入功能页面  ***********************/
        case 4: // 长按set&-（0页面下进入组态参数配置栏）
        {
            // if (Flag2.OneBit.Lock == 0)
            // {
            Flag2.OneBit.Lock = 1;
            Flag2.OneBit.Rt0Set = 0;
            Flag2.OneBit.PwRight = 0; // 密码状态归0

            m_PageInfor.m_Type = 0x01;  // 进入配置栏（P0显示）
            m_PageInfor.m_CfgPg = 0x00; // 密码栏

            DuplicatePara.password = 0;                                                                // 密码初始值
            memcpy((uint8_t *)&DuplicatePara.St, (uint8_t *)&cfgPara[CoolingMode].St, CfglistNum - 1); // 复制备用数据
            DuplicateData = CoolingMode;
            St_ParasRangeUpate_Evt(DuplicateData);

            DisplayCfgListCode(CfgList[m_PageInfor.m_CfgPg]);                                                          // 显示参数菜单栏
            Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
            // }
        }
        break;

        case 5: // 长按+&-,（0注页面下进入单一温度设置栏），解锁
            if (Flag2.OneBit.Lock == 1)
            {
                Flag2.OneBit.Lock = 0;

                Flag2.OneBit.Rt0Set = 1;
                Flag2.OneBit.PwRight = 0; // 密码状态归0

                m_PageInfor.m_Type = 0x02;                                                       // 参数设置栏 //进入配置栏（P0显示）
                m_PageInfor.m_CfgPg = 0x01;                                                      // 温度栏
                memcpy((uint8_t *)&DuplicatePara.St, (uint8_t *)&cfgPara[CoolingMode].St, 0x03); // 复制温度备用数据

                DuplicateData = CoolingMode;
                St_ParasRangeUpate_Evt(DuplicateData);
                DisplayCfgData(duplicate_sptr[m_PageInfor.m_CfgPg], CfgParaRange[m_PageInfor.m_CfgPg].dp);

                Timer_Event_Set(&timer_10msDB[CfgParaTimeout_TaskId], 15 * Second, ENABLE, 0, ExitCfgPageAndSavePara_Evt); // 开启超时事件
            }
            break;

        case 6:                                                              // 长按set&+,（强制化霜）
            if (QuickFrozenState == q_Idle && Flag3.OneBit.factoryMode == 0) // 压缩机上电延时结束后
            {
                if (ErrorState.OneBit.Ntc0Error == 0 && ErrorState.OneBit.Ntc1Error == 0) // 柜内传感器正常下进入化霜
                {
                    /******************************************收到进入强制除霜******************************************/
                    if (DefrostTemp < (cfgPara[CoolingMode].defrosteEndTemp * 10) || cfgPara[CoolingMode].defrosteType == 0)
                    {
                        DefrostStart_Evt();
                    }
                }
            }
            break;

        case 7:                                                                                                 // 长按set&-,进入工厂测试阶段
            if (QuickFrozenState == q_Idle && DefrostState == defrostingCycle && Flag3.OneBit.factoryMode == 0) // 压缩机上电延时结束后
            {
                Flag3.OneBit.factoryModePart = 0;
                tPara.factoryModeTimeCount = 0;
                Flag3.OneBit.factoryMode = 1;
                L_CoolingMode = CoolingMode; // 测试冷冻模式下的参数
                CoolingMode = 0;
                ExitCfgPageAndSavePara_Evt();
            }
            break;

        default:
            break;
        }
        LongKeyEvt = 0;
    }
}
